<?php
class DB {
	var $host = '';
	var $user = '';
	var $password = '';
	var $database = '';
	var $persist = false;
	var $conn = NULL;
	var $result = false;
	var $fields;
	var $check_fields;
	var $tbname;
	var $addNewFlag = false;
	var $new = false;
	var $opened = false;
	
	public function DB($host = "", $user = "", $password = "", $dbname = "", $open = false, $new = false) {
		($host != "") && ($this->host = $host);
		($user != "") && ($this->user = $user);
		($password != "") && ($this->password = $password);
		($dbname != "") && ($this->database = $dbname);
		$this->new = $new;
		$open && $this->open ();
	}
	
	public function open($host = "", $user = "", $password = "", $dbname = "") {
		if (! $this->opened) {
			($host != "") && ($this->host = $host);
			($user != "") && ($this->user = $user);
			($password != "") && ($this->password = $password);
			($dbname != "") && ($this->database = $dbname);
			
			$this->opened = true;
			
			if ($this->connect () === false) {
				return false;
			}
			if ($this->select_db () === false) {
				return false;
			}
		}
		return $this->conn;
	}
	
	public function set_host($host, $user, $password, $dbname) {
		$this->host = $host;
		$this->user = $user;
		$this->password = $password;
		$this->database = $dbname;
	}
	
	public function affectedRows() {
		return @mysql_affected_rows ( $this->conn );
	}
	
	public function close() {
		if ($this->opened) {
			$this->opened = false;
			@mysql_close ( $this->conn );
		}
	}
	
	public function connect() {
		if (is_resource ( $this->conn )) {
			return true;
		}
		if ($this->persist) {
			($this->conn = @mysql_pconnect ( $this->host, $this->user, $this->password )) or $this->errorlog ( 'mysql_pconnect' );
		} else {			
			($this->conn = @mysql_connect ( $this->host, $this->user, $this->password, $this->new )) or $this->errorlog ( 'mysql_connect' );
		}
		if (! is_resource( $this->conn )) {
			return false;
		} elseif (mysql_get_server_info () > '4.1') {
			$this->query( "SET SQL_MODE='',CHARACTER_SET_CONNECTION='utf8',CHARACTER_SET_RESULTS='utf8',CHARACTER_SET_CLIENT='binary',NAMES 'utf8'" );
		}
		return true;
	}
	
	public function select_db($dbname = "") {
		$dbname = $this->database = (($dbname == "") ? $this->database : $dbname);
		return $dbname == '' ? true : @mysql_select_db( $dbname, $this->conn );
	}
	
	public function create_db($dbname = "") {
		$dbname = $this->database = (($dbname == "") ? $this->database : $dbname);
		return $this->query( "CREATE DATABASE " . $dbname );
	}
	
	public function drop_db($dbname = "") {
		$dbname = $this->database = (($dbname == "") ? $this->database : $dbname);
		return $this->query( "DROP DATABASE " . $dbname );
	}
	
	public function error() {
		return @mysql_error ();
	}
	
	public function errorno() {
		return @mysql_errno ();
	}
	
	public function query($sql = '') {
		$this->opened or $this->open ();
		($this->result = @mysql_query( $sql, $this->conn )) or $this->errorlog( $sql );
		return $this->result;
	}
	
	public function numRows($result = null) {
		is_resource( $result ) or ($result = $this->result);
		return @mysql_num_rows( $result );
	}
	
	public function insertID() {
		return @mysql_insert_id( $this->conn );
	}
	
	/**
	 * 获取单条数据
	 */
	public function fetchRow($sql) {
		$result = $this->query( $sql );
		return @mysql_fetch_row( $result );
	}
	
	/**
	 * 获取单条数据
	 */
	public function fetchObject($sql) {
		$result = $this->query( $sql );
		return @mysql_fetch_object( $result );
	}
	
	/**
	 * 获取单条数据
	 */
	public function fetchArray($sql, $mode = MYSQL_BOTH) {
		$result = $this->query( $sql );
		$array = @mysql_fetch_array( $result, $mode );
		return is_array( $array ) ? $array : array ();
	}
	
	/**
	 * 获取单条对象数据
	 */
	public function fetchAssoc($sql) {
		$result = $this->query( $sql );
		return @mysql_fetch_assoc( $result );
	}
	
	public function freeResult($result = null) {
		is_resource( $result ) or ($result = $this->result);
		return @mysql_free_result( $result );
	}
	
	/**
	 * 更新数据
	 */
	public function update($tbl, $aSet, $aWhere = array()) {
		if (! $aSet || ! $tbl) {
			return false;
		}
		foreach( ( array ) $aSet as $field_name => $value ) {
			$strSet = $field_name . "='" . $value . "',";
		}
		$strSet = substr( $strSet, 0, strlen( $strSet ) - 1 );
		
		$qry = "UPDATE " . $tbl . " SET " . $strSet;
		if (count( $aWhere ) > 0 && is_array( $aWhere )) {
			$sWhere .= " WHERE ";
			foreach( $aWhere as $field_name => $value ) {
				$sWhere .= $field_name . "='" . $value . "' AND ";
			}
			$sWhere = substr( $sWhere, 0, strlen( $sWhere ) - 5 );
			$qry .= $sWhere;
		}
		return $this->query( $qry );
	}
	
	/**
	 * 插入数据
	 */
	public function insert($tbl, $aSet) {
		if (! $aSet || ! $tbl) {
			return false;
		}
		$sql = "INSERT INTO $tbl SET ";
		foreach( ( array ) $aSet as $key => $value ) {
			$sql .= "`$key`='$value',";
		}
		$this->query( substr( $sql, 0, - 1 ) );
		return $this->insertID ();
	}
	
	/**
	 * 获取所有的数据列表
	 */
	public function getAll($sql, $mode = MYSQL_BOTH) {
		$result = $this->query( $sql );
		while( $row = @mysql_fetch_array( $result, $mode ) ) {
			$aT [] = $row;
		}
		return( array ) $aT;
	}
	
	/**
	 * 加上双引号
	 */
	public function escape($string) {
		return mysql_escape_string( trim( $string ) );
	}
	
	/**
	 * 去掉双引号
	 */
	public function unescape($string) {
		return stripslashes( $string );
	}
	
	/**
	 * 开始事务
	 */
	public function Start() {
		$this->query( "START TRANSACTION" );
	}
	
	/**
	 * 提交事务
	 */
	public function Commit() {
		$this->query( "COMMIT" );
	}
	
	/**
	 * 回滚事务
	 */
	public function Rollback() {
		$this->query( "ROLLBACK" );
	}
	
	/**
	 * 错误 日志的输入
	 */
	private function errorlog($msg = '') {
		$error = date( 'Y-m-d H:i:s' ) . ":\n:" . $this->errorno () . $this->error () . $msg . ";\n";
		$file = dirname( __FILE__ ) . '/../../data/sqlerr.txt';
		@file_put_contents( $file, "{$error}\n", @filesize( $file ) < 32 * 1024 ? FILE_APPEND : null );
		die( 'DB Invalid!!!' );
	}
	
	/**
	 * 关闭MYSQL连接
	 */
	public function __destruct() {
		$this->close();
	}
}